<template>
	<view class="main">
		<view class="flex align between topSearch">
			<view style="width: 83%;">
				<u-search placeholder="输入卡片名搜索" bgColor="#fff" :showAction="false" v-model="merge_keyword" @change="change"></u-search>
			</view>
			<view class="addBtn flex align" @click="toUrl()">添加<uni-icons type="plus" size="20"
					color="#fff"></uni-icons></view>
		</view>
		<view class="list" v-for="(item,index) in list" :key="index">
			<view class="list-name flex align between">
				<view>{{item.title}}</view>
				<view v-if="item.status==1">正常</view>
				<view v-else style="background: #D10000;">关闭</view>
			</view>
			<view class="infos flex between">
				<view class="info-item">
					<view class="flex align between item" v-if="item.type==1">
						<view>名片</view>
						<view>
							<image @click="toPreImg(item.head_url)" :src="item.head_url" style="width: 80rpx;height: 80rpx;"></image>
						</view>
					</view>
					<view class="flex align between item">
						<view>类型</view>
						<view>{{item.type==1?'微信名片':'h5链接'}}</view>
					</view>
					<view class="flex align between item">
						<view>添加时间</view>
						<view>{{item.addtime}}</view>
					</view>
					<view class="flex align between item">
						<view>到期时间</view>
						<view>{{item.out_date}}</view>
					</view>
					<view class="flex align between item">
						<view>链接</view>
						<view>{{item.url}}</view>
					</view>
					<view class="flex align between item" v-if="item.type==2">
						<view>H5链接</view>
						<view>{{item.h5_url}}</view>
					</view>
				</view>
				<view class="rank">
					<view>剩余次数</view>
					<view>{{item.use_count}}</view>
				</view>
			</view>
			<view class="btns flex align between">
				<view class="trash flex align" @click="toEdit(item)"><uni-icons color="#fff" type="compose" size="25"></uni-icons>编辑</view>
				<view class="trash flex align" @click="toDel(item.id)"><uni-icons color="#fff" type="trash" size="25"></uni-icons>删除</view>
			</view>
		</view>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
	</view>
</template>

<script>
	import {
		storeCardIndex,
		mergeKeywordAdd,
		storeCardDel
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				keyword: '',
				show:false,
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				merge_keyword:''
			};
		},
		onLoad() {
			// console.log(decodeURIComponent('%7B%22merge_keyword%22%3A%22%E4%BA%91%E5%AA%92%22%7D'))
		},
		onShow() {
			this.page = 0
			this.list = []
			this.getlist();
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods:{
			toEdit(item){
				let str = JSON.stringify(item)
				uni.navigateTo({
					url:'/pages/private/add?item='+str
				})
			},
			toPreImg(img){
				uni.previewImage({
					urls:[img],
					current:1
				})
			},
			// 删除
			toDel(id){
				let that = this
				uni.showModal({
					title: "提示",
					content: "确定要删除吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.delCom(id)
						} else {
							
						}
					}
				
				})
			},
			async delCom(id){
				let obj = {
					action: 'del',
					ids: id
				}
				const result = await storeCardDel(obj)
				if(result.code==1){
					uni.showToast({
						title:'删除成功'
					})
					this.page = 0
					this.list = []
					this.getlist()
				}else{
					uni.showToast({
						title:result.msg,
						icon:'none'
					})
				}
			},
			change(){
				this.page = 0
				this.list = []
				this.getlist()
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					merge_keyword
				} = that;
			
				let obj = {
					menu_url:'card',
					limit: offset,
					offset: page,
					sort: 'id',
					order: 'desc',
					_:new Date().getTime()
					// filter:encodeURIComponent(JSON.stringify(filter)),
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				if(merge_keyword!=''){
					obj.filter = JSON.stringify({"title":merge_keyword.toString()})
					obj.op = JSON.stringify({"title":'='})
				}
				that.status = 'loading';
				if (totalPage > page) {
					let result = await storeCardIndex(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
			toUrl(){
				uni.navigateTo({
					url:'/pages/private/add'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 13rpx 30rpx;

		.topSearch {
			margin-bottom: 14rpx;

			.addBtn {
				background: #FFCDB8;
				color: white;
				border-radius: 14rpx;
				font-size: 20rpx;
				padding: 8rpx 14rpx;
			}
		}

		.list {
			background: white;
			border-radius: 15rpx;
			margin-top: 20rpx;
			padding-bottom: 35rpx;

			.list-name {
				background: #2D2D2D;
				border-radius: 15rpx 15rpx 0rpx 0rpx;
				padding: 26rpx 50rpx;

				view:nth-child(1) {
					color: #FFE9E0;
					font-size: 28rpx;
				}

				view:nth-child(2) {
					background: #00ACFF;
					color: white;
					padding: 1rpx 17rpx;
					border-radius: 40rpx;
				}
			}

			.infos {
				padding: 38rpx;

				.info-item {
					width: 60%;

					.flex {
						margin-bottom: 5rpx;

						view:nth-child(1) {
							font-size: 24rpx;
							width: 30%;
						}

						view:nth-child(2) {
							font-size: 20rpx;
							font-weight: bold;
							width: 70%;
						}
					}
				}

				.rank {
					text-align: center;

					view:nth-child(1) {
						font-size: 32rpx;
						font-weight: bold;
					}

					view:nth-child(2) {
						font-size: 32rpx;
						font-weight: bold;
						background: #FFCDB8;
						min-width: 128rpx;
						height: 68rpx;
						line-height: 68rpx;
						text-align: center;
						color: #D10000;
						border-radius: 10rpx;
						margin-top: 19rpx;
						padding: 0 10rpx;
					}
				}
			}

			.btns {
				justify-content: space-around;

				.trash {
					justify-content: center;
					background: #2D2D2D;
					color: white;
					font-size: 32rpx;
					font-weight: bold;
					padding: 18rpx 38rpx;
					border-radius: 15rpx;

				}
			}


		}
	}
</style>